import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";

import {Observable} from "rxjs/Observable";
import * as moment from 'moment';

import {
    ScheduleItem,
    ScheduleFormTip,
    TimeFormat,
    TimerItem,
    limitTime
} from '../../../model/pages/schedule/schedule-add/schedule-add.model';

@Injectable()
export class ScheduleAddProvider {

    public differTime: number = 8 * 60 * 60 * 1000;
    public baseUrl='/reminder/v1';

    constructor(public http: HttpClient) {

    }

    // 提交验证标题
    public checkFormData(newItem: ScheduleItem) {
        if (!newItem.name||!newItem.name.trim()) {
            return '请输入日程标题';
        } else {
            return false;
        }
    }

    // 新建初始化时间：1、date时间戳；2、flag是否当日
    public timeFormat(date:number,flag:boolean) {
        if(typeof date != 'number'){
            return false
        }
        const currMinute = moment().minute();
        const currHour=moment().hour();
        const setMinute=currMinute <= 30?this.minuteToMs(30):this.minuteToMs(60);
        const todayMs=this.hourToMs(currHour)+setMinute;

        let currectValue = 0;
        if(currHour==23||(currHour==22&&currMinute>30)){
            currectValue = date+this.hourToMs(22)+this.minuteToMs(55);
        }else{
            currectValue = date +todayMs;
        }

        let result: TimeFormat = new TimeFormat();
        result.ionTime = new Date(currectValue + this.differTime).toISOString();
        result.showTime = new Date(currectValue ).toISOString();
        return result;
    }

    // 详情回填时间转化为ISO时间
    public getISOString(startDate:number, endDate:number) {
        let isoStr: TimerItem = new TimerItem();
        isoStr.startTimeStr = new Date(startDate + this.differTime).toISOString();
        isoStr.endTimeStr = new Date(endDate + this.differTime).toISOString();
        return isoStr;
    }

    // 限定开始时间、结束时间：1、不能跨天；2、由开始时间来限定结束时间；3、
    public limitTimeRange(startDate) {
        const start=startDate;
        const startTime=new Date(startDate);
        const hour = startTime.getHours();
        const minute = startTime.getMinutes();
        const dayMaxDate = this.hourToMs(23) + this.minuteToMs(55);

        let rangeDate: limitTime = new limitTime();
        rangeDate.maxTime = new Date((start - this.hourToMs(hour) - this.minuteToMs(minute) + dayMaxDate + this.differTime)).toISOString();
        if(hour == 23){
            rangeDate.ionTime = rangeDate.maxTime;
            rangeDate.unixTime=start - this.hourToMs(hour) - this.minuteToMs(minute) + dayMaxDate;
        }else{
            rangeDate.ionTime = new Date(start + this.minuteToMs(60)+ this.differTime).toISOString();
            rangeDate.unixTime=start + this.minuteToMs(60);
        }
        return rangeDate;
    }

    // 添加日程
    public postNewScheduleItem(item: ScheduleItem): Observable<any> {
        return this.http.post('/reminderNew/v2', item);
    }
    // 修改日程
    public updateScheduleItem(id:number,item: ScheduleItem): Observable<any> {
        return this.http.put('/reminderNew/v2'+'/'+id,item,{responseType:'text'});
    }
    // 删除日程
    public deleteScheduleItem(id:number,appointDay :number,updateRule :number):Observable<any>{
        return this.http.delete('/reminderNew/v2'+'/'+id+'/'+appointDay+'/'+updateRule,{responseType:'text'});
    }

    // 获取日程详情
    public getScheduleDetail(id: number,date:number): Observable<any> {
        return this.http.get('/reminderNew/v2'+'/'+id+'/'+date/1000);
    }

    // 计算小时毫秒数
    private hourToMs(hour){
        return hour*60*60*1000;
    }
    // 计算分钟级毫秒数
    private minuteToMs(minute){
        return minute*60*1000;
    }
    // 计算秒级毫秒数
    private secondToMs(second){
        return second*1000;
    }

    // 计算年度时间范围
    getYearList(min){
        let list=[];
        for(let y=min;y<=2100;y++){
            list.push(y);
        }
        return list;
    }
}
